$(function() {
  var err=$('#neterror').css('display','none');
  if(!isLocalStorageNameSupported()){
    new UserPrompt('您的运行环境不支持本地离线缓存');
    return;
  }
  var search=decodeURI(window.location.search.substr(1)),
  loadPrompt,
  callerInfo=matchQueryString(search,'caller'),
  showCTMode=matchQueryString(search,'showCTMode'),//控制通话和留言功能显示
  companyInfo=matchQueryString(search,'authData'),
  accountIdentify=matchQueryString(search,'cust_account'),
  accountIdentifer=matchQueryString(search,'app_key'),
  isadmin=matchQueryString(search,'isadmin'),
  isAdmin=(isadmin===1||isadmin==='1')?true:false,//不设置isadmin参数默认为非管理员
  index=matchQueryString(search,'index'),
  supportUap=matchQueryString(search,'supportUap')||'0',
  isLoadContacts=matchQueryString(search,'isLoadContacts')||'0',
  showAlipay=matchQueryString(search,'showAlipay'),
  showUnionAccount=matchQueryString(search,'showUnionAccount'),
  showMoreBtn=matchQueryString(search,'showMoreBtn'),
  isShowAlipay=(showAlipay===-1||showAlipay==='-1'||showAlipay==='0'||showAlipay===0)?true:false,//设置是否显示支付宝支付
  isShowUnionAccount=(showUnionAccount===1||showUnionAccount==='1')?true:false,//不设置showUnionAccount参数默认显示账户合并页面
  color=matchQueryString(search,'color'),
  contacts=matchQueryString(search,'called');
  if(color){
	  localCache.setSessionItems({
		  color:'#'+color
	  });
  }
  localCache.load(true);//加载基本缓存参数  
  if(showCTMode!=='1'&&showCTMode!=='2'&&showCTMode!=='12')
	  showCTMode='12';
  if(showMoreBtn==='')
	  showMoreBtn='123';
  var callerObject=getJSONParsedObject(callerInfo,'主叫信息参数格式错误'),
  companyObject=getJSONParsedObject(companyInfo,'组织信息参数格式错误');
  var called=getJSONParsedObject(contacts,'被叫信息参数格式错误'); 
  var base={
      type: "post",
      url: localCache.getSessionItem('bs_url'),  
      dataType: "json",
      async:true,
      data:{
    	cust_account:accountIdentify,
        app_key:accountIdentifer,
        sign:matchQueryString(search,'sign'),
        timestamp:matchQueryString(search,'timestamp'),
        caller:callerInfo,
        authData:companyInfo,
        isadmin:isadmin
      },
      complete:function(xhr,status){
        if(loadPrompt){
          loadPrompt.close();
          loadPrompt=null;
        }
      },
      beforeSend:function(){
        loadPrompt=new LoadPrompt('正在校验账户信息，请稍后...');           
      },
      success: function(rc){
        if(rc.code==='0000'||rc.code==='0017'||rc.code==='0019'){//校验通过\余额不足\管理员登录没有配置充值相关信息
          //根据index值路由页面，默认进入欢迎页面
          index=(index==='')?0:Number(index);
          //调用接口缓存用户数据
          localCache.setSessionItems({
        	showCTMode:showCTMode,
        	showMoreBtn:showMoreBtn,
            account_identifer:accountIdentifer,
            timestamp:rc.timestamp,
            sign:rc.sign,
            periphery_code:rc.periphery_code,
            periphery_account:rc.cust_account,
            caller:callerObject.telephone,
            callerName:callerObject.name,
            account_identify:rc.cust_account,
            companyName:companyObject.ext_orgName,
            userId:meetCache.userId,  
            isAdmin:isAdmin,
            index:index,
            contacts:contacts,
            supportUap:supportUap,
            isLoadContacts:isLoadContacts,
            isShowAlipay:isShowAlipay,
            isShowUnionAccount:isShowUnionAccount
          });
          if(rc.code==='0017')//余额不足
        	  index=isAdmin?6:7;
          if(rc.code==='0019'){//未设置充值信息,充值功能无法使用
              new UserPrompt(rc.describe,function(){
                  router(index);        
              });  
              return;
          }else{
              if(isAdmin){//登录人员为管理员时，缓存组织编码、产品编码、区域编码，卡充值时使用
                  localCache.setSessionItems({
                    company_code:rc.company_code,
                    product_code:rc.product_code,
                    region_code:rc.region_code
                  });
              }
          }
          if(index===0&&meetCache.hasMeetStorage())//存在通话记录
        	  index=10;
//          
//          var str='[';  
//          var m=[];
//          for(var i=0;i<50;i++){
//        	  m.push('{"name":"zhangsan","telephone":"1700102'+i+'"}');   
//          }
//          str+=m.join(',')+']';
//          localCache.setSessionItem('contacts',str);  
          if(index===11){
            router(index,window.location.search+'&isAuth=1');//直接跳转到两方拨号页面
          }else if(index===12){
 		   var dataObject={
     			data:[{
     				name:callerObject.name,
     				telephone:callerObject.telephone,
     				type:'main'
     			}]     		
 		   };
 		    Array.prototype.push.apply(dataObject.data,called);
            router(index,window.location.search+'&data='+JSON.stringify(dataObject));//直接跳转到两方拨号页面
          }else
            router(index);  
      }else if(rc.code==='0015'){//账户不存在，需要进入注册页面	
    	router(8);
      }else{//其他参数异常
        new UserPrompt(rc.describe); 
      }
     }
  };    
      
  err.on('click',function(){
    dispatch(null);
  });

  function dispatch(parameters){
    $.ajax($.extend({},base,parameters));
  }
  
  dispatch({
    error:function(xhr,status,error){
      if(status==='error'){
          handlerError('netconnectionerror',function(){
            err.css('display','');
        });           
      }else{
        //调用接口异常
      }
   }
  }); 

 }
)



